<?php
class DocumentsController extends AppController {

	var $name = 'Documents' ;
	var $helpers = array('Html', 'Form', 'Authake.Authak3');
	
	function index () {
		// для установки факультета и курса
		$this->loadModel('Facultet');
		/* 		$facultets = $this->Facultet->find('list');
		 $this->set(compact('facultets')); */
		// читаем из сессии какие факультеты доступны
		$facultetsettings = $this->Session->read('FacultetsSettings');
		
		$tmpfacultets = $this->Facultet->find('all', array('conditions' => array('Facultet.id' => $facultetsettings), 'recursive' => -1));
		//debug($facultetsettings);
		foreach ($tmpfacultets as $facultet):
		$facultets[$facultet['Facultet']['id']] = $facultet['Facultet']['name'];
		endforeach;
		$this->set(compact('facultets'));
		
		$this->loadModel('Profession');
		$selectedprofessions = $this->Profession->find('list');
		$this->set(compact('selectedprofessions'));
		$this->loadModel('Course');
		$selectedcourses = $this->Course->find('list');
		$this->set(compact('selectedcourses'));
		$this->loadModel('Studentgroup');
		$selectedgroups = $this->Studentgroup->find('list');
		$this->set(compact('selectedgroups'));
		// вывод установки если есть
		// иницилизация переменных настроек
		$selectedfacultet = '';
		$selectedprofession = '';
		$selectedcourse = '';
		$selectedgroup = '';
		if ($this->Session->check('Settings')){
			$settings = $this->Session->read('Settings');
			$selectedfacultet = $settings['Settings']['selectedfacultet'];
			$selectedprofession = $settings['Settings']['selectedprofession'];
			$selectedcourse = $settings['Settings']['selectedcourse'];
			$selectedgroup = $settings['Settings']['selectedgroup'];
		}
		$this->set(compact('selectedfacultet', $selectedfacultet));
		$this->set(compact('selectedprofession', $selectedprofession));
		$this->set(compact('selectedcourse', $selectedcourse));
		$this->set(compact('selectedgroup', $selectedgroup));		
	}
	/*
	 * установка отображения студентов
	 */
	function settings_set() {
		if (!empty($this->data)) {
			$this->Session->delete('Settings');
			$selectedfacultet = '';
			$selectedprofession = '';
			$selectedcourse = '';
			$selectedgroup = '';
			if ($this->data['Document']['facultet_id']) {
				$selectedfacultet = $this->data['Document']['facultet_id'];
			}
			if ($this->data['Document']['profession_id']) {
				$selectedprofession = $this->data['Document']['profession_id'];
			}
			if ($this->data['Document']['course_id']) {
				$selectedcourse = $this->data['Document']['course_id'];
			}
			if ($this->data['Document']['studentgroup_id']) {
				$selectedgroup = $this->data['Document']['studentgroup_id'];
			}
			$settings = array('selectedfacultet' => $selectedfacultet,
									'selectedprofession' => $selectedprofession,
									'selectedcourse' => $selectedcourse,
									'selectedgroup' => $selectedgroup);
			$sessionsettings = array ('Settings' => $settings );
			$this->Session->write('Settings', $sessionsettings);
		}
		//	debug($this->data['Student']);
		$this->redirect(array('action' => 'index'));
	}
	/*
	 * сброс настроек отображения студентов
	 */
	function settings_unset() {
		if (!empty($this->data)) {
			$this->Session->delete('Settings');
		}
		$this->redirect(array('action' => 'index'));
	}
	/*
	 * ajax функции для формироавния отображения студентов
	 */
	function update_profession () {
	
		if(!empty($this->data['Document']['facultet_id'])) {
			$facultet_id = $this->data['Document']['facultet_id'];
			$this->loadModel('Profession');
			$options = $this->Profession->find('list', array('conditions' => array ('Profession.facultet_id' => $facultet_id)));
			$this->set(compact('options'));
		}
	}
	
	function update_course () {
		if(!empty($this->data['Document']['profession_id'])) {
			$profession_id = $this->data['Document']['profession_id'];
			$this->loadModel('Course');
			$options = $this->Course->find('list', array('conditions' => array ('Course.profession_id' => $profession_id)));
			$this->set(compact('options'));
		}
	}
	
	function update_studentgroup () {
		if(!empty($this->data['Document']['course_id'])) {
			$course_id = $this->data['Document']['course_id'];
			$this->loadModel('Studentgroup');
			$options = $this->Studentgroup->find('list', array('conditions' => array ('Studentgroup.course_id' =>  $course_id)));
			$this->set(compact('options'));
		}
	}
	/***********************конец ajax функций *******************************/
	/*
	 * формирование документов стартовая страница
	 */
	function documentsindex() {
		// для установки факультета и курса
		$this->loadModel('Facultet');
		/* 		$facultets = $this->Facultet->find('list');
		 $this->set(compact('facultets')); */
		// читаем из сессии какие факультеты доступны
		$facultetsettings = $this->Session->read('FacultetsSettings');
		
		$tmpfacultets = $this->Facultet->find('all', array('conditions' => array('Facultet.id' => $facultetsettings), 'recursive' => -1));
		//debug($facultetsettings);
		foreach ($tmpfacultets as $facultet):
		$facultets[$facultet['Facultet']['id']] = $facultet['Facultet']['name'];
		endforeach;
		$this->set(compact('facultets'));
		
		$this->loadModel('Profession');
		$selectedprofessions = $this->Profession->find('list');
		$this->set(compact('selectedprofessions'));
		$this->loadModel('Course');
		$selectedcourses = $this->Course->find('list');
		$this->set(compact('selectedcourses'));
		$this->loadModel('Studentgroup');
		$selectedgroups = $this->Studentgroup->find('list');
		$this->set(compact('selectedgroups'));
		// вывод установки если есть
		// иницилизация переменных настроек
		$selectedfacultet = '';
		$selectedprofession = '';
		$selectedcourse = '';
		$selectedgroup = '';
		if ($this->Session->check('Settings')){
			$settings = $this->Session->read('Settings');
			$selectedfacultet = $settings['Settings']['selectedfacultet'];
			$selectedprofession = $settings['Settings']['selectedprofession'];
			$selectedcourse = $settings['Settings']['selectedcourse'];
			$selectedgroup = $settings['Settings']['selectedgroup'];
		}
		$this->set(compact('selectedfacultet', $selectedfacultet));
		$this->set(compact('selectedprofession', $selectedprofession));
		$this->set(compact('selectedcourse', $selectedcourse));
		$this->set(compact('selectedgroup', $selectedgroup));		
		
		if ($selectedcourse){
			$groups = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.course_id' => $selectedcourse), 'recursive' => -1));
			$groupslist = array();
			foreach ($groups as $group):
			$groupslist[$group['Studentgroup']['id']]= $group['Studentgroup']['name'];
			endforeach;
		} else {
			$groupslist = '';
		}
		$this->set('groups', $groupslist);
	}
	/*
	 * справка студенту
	 */
	function makespravka() {
		// для установки факультета и курса
		$this->loadModel('Facultet');
		/* 		$facultets = $this->Facultet->find('list');
		 $this->set(compact('facultets')); */
		// читаем из сессии какие факультеты доступны
		$facultetsettings = $this->Session->read('FacultetsSettings');
		
		$tmpfacultets = $this->Facultet->find('all', array('conditions' => array('Facultet.id' => $facultetsettings), 'recursive' => -1));
		//debug($facultetsettings);
		foreach ($tmpfacultets as $facultet):
		$facultets[$facultet['Facultet']['id']] = $facultet['Facultet']['name'];
		endforeach;
		$this->set(compact('facultets'));
		
		$this->loadModel('Profession');
		$selectedprofessions = $this->Profession->find('list');
		$this->set(compact('selectedprofessions'));
		$this->loadModel('Course');
		$selectedcourses = $this->Course->find('list');
		$this->set(compact('selectedcourses'));
		$this->loadModel('Studentgroup');
		$selectedgroups = $this->Studentgroup->find('list');
		$this->set(compact('selectedgroups'));
		// вывод установки если есть
		// иницилизация переменных настроек
		$selectedfacultet = '';
		$selectedprofession = '';
		$selectedcourse = '';
		$selectedgroup = '';
		if ($this->Session->check('Settings')){
			$settings = $this->Session->read('Settings');
			$selectedfacultet = $settings['Settings']['selectedfacultet'];
			$selectedprofession = $settings['Settings']['selectedprofession'];
			$selectedcourse = $settings['Settings']['selectedcourse'];
			$selectedgroup = $settings['Settings']['selectedgroup'];
		}
		$this->set(compact('selectedfacultet', $selectedfacultet));
		$this->set(compact('selectedprofession', $selectedprofession));
		$this->set(compact('selectedcourse', $selectedcourse));
		$this->set(compact('selectedgroup', $selectedgroup));

		$this->loadModel('Student');
		$this->loadModel('Studentinfo');
		
		if ($selectedgroup){
			$students = $this->Student->find('all', array('conditions' => array('Student.studentgroup_id' => $selectedgroup)));
			$studentslist = array();
			foreach ($students as $student):
			$studentslist[$student['Student']['id']]= $student['Student']['lastname'].' '.$student['Student']['firstname'].' '.$student['Student']['surname'];
			endforeach;
		} else {
			$studentslist = '';
		}
		$this->set('students', $studentslist);
	}
	
	/*
	 * генерация word файла в альбомной ориентации с позиционированием справки
	 */
	function spravka() {
		if (!empty($this->data)){
			$position = $this->data['Document']['position'];
			
			$student_id = $this->data['Document']['student_id'];
			$this->loadModel('Student');
			$student = $this->Student->find('all', array('conditions' => array('Student.id' => $student_id)));
				
			$this->set('student', $student);
			
			$this->loadModel('Course');
			$courses = $this->Course->find('list');
			$this->set(compact('courses'));
			
			$this->loadModel('Facultet');
			$facultets = $this->Facultet->find('list');
			$this->set(compact('facultets'));
			
			if ($this->Session->check('Settings')){
				$settings = $this->Session->read('Settings');
				$selectedfacultet = $settings['Settings']['selectedfacultet'];
				$selectedprofession = $settings['Settings']['selectedprofession'];
			}
			$this->set(compact('selectedfacultet', $selectedfacultet));
			
			switch ($position){
				case 0: $this->render('/', 'blank', 'spravkatopleft');
				break;
				case 1: $this->render('/', 'blank', 'spravkatopright');
				break;
				case 2: $this->render('/', 'blank', 'spravkabottomleft');
				break;
				case 3: $this->render('/', 'blank', 'spravkabottomright');
				break;
			}
		}
	}
	/*
	 * вывод всех студентов факультета для генерации справок отмеченным студентам
	 */
	function makespravki() {
		
		$this->loadModel('Facultet');
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Student');
		// читаем из сессии какие факультеты доступны
		$facultetsettings = $this->Session->read('FacultetsSettings');
		
		$tmpfacultets = $this->Facultet->find('all', array('conditions' => array('Facultet.id' => $facultetsettings), 'recursive' => -1));
		//debug($facultetsettings);
		$preparedstudents = array();
		foreach ($tmpfacultets as $facultet):
		// $facultet['Facultet']['name'];	
			$selectedprofessions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet['Facultet']['id']), 'recursive' => 0));
			foreach ($selectedprofessions as $selectedprofession):
			//array_push($selectedprofessions_ids, $element['Profession']['id']);
				$selectedcourses = $this->Course->find('all', array('conditions' => array('Course.profession_id' => $selectedprofession['Profession']['id']),
											'recursive' => 0, 'order' => 'Course.id ASC'));
				foreach ($selectedcourses as $selectedcourse):
					//array_push($selectedcourses_ids, $element['Course']['id']);
					$selectedgroups = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.course_id' => $selectedcourse['Course']['id']),
												'recursive' => 0, 'order' => 'Studentgroup.id ASC'));
					foreach ($selectedgroups as $selectedgroup):
					//array_push($selectedgroups_ids, $element['Studentgroup']['id']);
						$students = $this->Student->find('all', array('conditions' => array('Student.studentgroup_id' => $selectedgroup['Studentgroup']['id'],
																							'Student.studentstate_id' => 1), 'recursive' => 0, 'order' => 'Student.id ASC'));
						foreach ($students as $student):
							$student['facultet'] = $facultet['Facultet']['name'];
							$student['profession'] =  $selectedprofession['Profession']['name'];
							$student['course'] = $selectedcourse['Course']['name'];
							$student['group'] = $selectedgroup['Studentgroup']['name'];		
							array_push($preparedstudents, $student);
						endforeach;
					endforeach;
				endforeach;
			endforeach;	
		endforeach;
		$this->set('students', $preparedstudents);
	}
	/*
	 * генерирует файл word со справками отмеченный студентам
	 */
	function spravki() {
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Student');
		
		$tmpstudents_ids = $this->data['students'];
		$students_ids = array();
		foreach($tmpstudents_ids as $key => $value):
		if ($value){
			array_push($students_ids, $value);
		}
		endforeach;
		
		$students = $this->Student->find('all', array('conditions' => array('Student.id' => $students_ids)));
		
		$allstudents = array();
		
		foreach ($students as $student):
			$group = $this->Studentgroup->find('first', array('conditions' => array('Studentgroup.id' => $student['Student']['studentgroup_id']), 'recursive' => 0));
			//debug($group);	
			$course = $this->Course->find('first', array('conditions' => array('Course.id' => $group['Studentgroup']['course_id']), 'recursive' => 0));
			//debug($course);
			$profession = $this->Profession->find('first', array('conditions' => array('Profession.id' => $course['Course']['profession_id']), 'recursive' => 0));
			//debug($profession);
			$student['facultet'] = $profession['Facultet']['name'];
			$student['profession'] = $profession['Profession']['name'];
			$student['course'] = $course['Course']['name'];
			$student['group'] = $group['Studentgroup']['name'];
			
			array_push($allstudents, $student);
		endforeach;
		
		$this->set('students', $allstudents);
		$this->render('/', 'blank', 'spravki');
	}
	/*
	 * генерирует бланк посещения студентами группы лекций по предмету
	 */
	function blankforlections (){
		if ($this->data){
			$this->loadModel('Profession');
			$this->loadModel('Course');
			$this->loadModel('Studentgroup');
			$this->loadModel('Student');
			$group_id = $this->data['Document']['group_id'];
			$group = $this->Studentgroup->find('first', array('conditions' => array('Studentgroup.id' => $group_id), 'recursive' => 0));
			$course = $this->Course->find('first', array('conditions' => array('Course.id' => $group['Studentgroup']['course_id']), 'recursive' => 0));
			$profession = $this->Profession->find('first', array('conditions' => array('Profession.id' => $course['Course']['profession_id']), 'recursive' => 0));
			
			$students = $this->Student->find('all', array('conditions' => array('Student.studentgroup_id' => $group_id, 'Student.studentstate_id' => 1), 'recursive' => 0));
			$this->set('profession', $profession);
			$this->set('course', $course);
			$this->set('group', $group);
			$this->set('students', $students);
			
			$this->render('/', 'blank', 'blankforlections');
		}
	}
	/*
	 * генерирует список студентов для формирования бланков на отработки
	 */
	function makeotrabotki() {
		$this->loadModel('Facultet');
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Student');

		$this->loadModel('Department');
		
		$departments = $this->Department->find('list');
		
		$this->set(compact('departments'));
		
		// читаем из сессии какие факультеты доступны
		$facultetsettings = $this->Session->read('FacultetsSettings');
		
		$tmpfacultets = $this->Facultet->find('all', array('conditions' => array('Facultet.id' => $facultetsettings), 'recursive' => -1));
		//debug($facultetsettings);
		$preparedstudents = array();
		foreach ($tmpfacultets as $facultet):
		// $facultet['Facultet']['name'];
		$selectedprofessions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet['Facultet']['id']), 'recursive' => 0));
		foreach ($selectedprofessions as $selectedprofession):
		//array_push($selectedprofessions_ids, $element['Profession']['id']);
		$selectedcourses = $this->Course->find('all', array('conditions' => array('Course.profession_id' => $selectedprofession['Profession']['id']),
													'recursive' => 0, 'order' => 'Course.id ASC'));
		foreach ($selectedcourses as $selectedcourse):
		//array_push($selectedcourses_ids, $element['Course']['id']);
		$selectedgroups = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.course_id' => $selectedcourse['Course']['id']),
														'recursive' => 0, 'order' => 'Studentgroup.id ASC'));
		foreach ($selectedgroups as $selectedgroup):
		//array_push($selectedgroups_ids, $element['Studentgroup']['id']);
		$students = $this->Student->find('all', array('conditions' => array('Student.studentgroup_id' => $selectedgroup['Studentgroup']['id'],
																									'Student.studentstate_id' => 1), 'recursive' => 0, 'order' => 'Student.id ASC'));
		foreach ($students as $student):
		$student['facultet'] = $facultet['Facultet']['name'];
		$student['profession'] =  $selectedprofession['Profession']['name'];
		$student['course'] = $selectedcourse['Course']['name'];
		$student['group'] = $selectedgroup['Studentgroup']['name'];
		array_push($preparedstudents, $student);
		endforeach;
		endforeach;
		endforeach;
		endforeach;
		endforeach;
		$this->set('students', $preparedstudents);
	}
	/*
	 * генерирует файл word с заполненными бланкам отработки
	 */
	function otrabotki () {
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Student');
		$this->loadModel('Department');
		// debug($this->data);
		// устанавливаем на какой кафедре отработки
		$department_id = $this->data['Document']['department_id'];
		$department = $this->Department->find('all', array('conditions' => array('Department.id' => $department_id)));
		$department = $department[0]['Department']['name'];
		$this->set('department', $department);
		
		
		$tmpstudents_ids = $this->data['students'];
		$students_ids = array();
		foreach($tmpstudents_ids as $key => $value):
		if ($value){
			array_push($students_ids, $value);
		}
		endforeach;
		
		$students = $this->Student->find('all', array('conditions' => array('Student.id' => $students_ids)));
		
		$allstudents = array();
		
		foreach ($students as $student):
		$group = $this->Studentgroup->find('first', array('conditions' => array('Studentgroup.id' => $student['Student']['studentgroup_id']), 'recursive' => 0));
		//debug($group);
		$course = $this->Course->find('first', array('conditions' => array('Course.id' => $group['Studentgroup']['course_id']), 'recursive' => 0));
		//debug($course);
		$profession = $this->Profession->find('first', array('conditions' => array('Profession.id' => $course['Course']['profession_id']), 'recursive' => 0));
		//debug($profession);
		$student['facultet'] = $profession['Facultet']['name'];
		$student['profession'] = $profession['Profession']['name'];
		$student['course'] = $course['Course']['name'];
		$student['group'] = $group['Studentgroup']['name'];
			
		array_push($allstudents, $student);
		endforeach;
		
		$this->set('students', $allstudents);
		

		$this->render('/', 'blank', 'otrabotki');
	}
	
	/*
	 *  генерация экзаменационного листа студенту
	 */
	function makeexamlist() {
		$this->loadModel('Facultet');
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Student');
		// читаем из сессии какие факультеты доступны
		$facultetsettings = $this->Session->read('FacultetsSettings');
		
		$tmpfacultets = $this->Facultet->find('all', array('conditions' => array('Facultet.id' => $facultetsettings), 'recursive' => -1));
		//debug($facultetsettings);
		$preparedstudents = array();
		foreach ($tmpfacultets as $facultet):
		// $facultet['Facultet']['name'];
		$selectedprofessions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet['Facultet']['id']), 'recursive' => 0));
			foreach ($selectedprofessions as $selectedprofession):
			//array_push($selectedprofessions_ids, $element['Profession']['id']);
			$selectedcourses = $this->Course->find('all', array('conditions' => array('Course.profession_id' => $selectedprofession['Profession']['id']),
														'recursive' => 0, 'order' => 'Course.id ASC'));
				foreach ($selectedcourses as $selectedcourse):
				//array_push($selectedcourses_ids, $element['Course']['id']);
				$selectedgroups = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.course_id' => $selectedcourse['Course']['id']),
																'recursive' => 0, 'order' => 'Studentgroup.id ASC'));
					foreach ($selectedgroups as $selectedgroup):
					//array_push($selectedgroups_ids, $element['Studentgroup']['id']);
					$students = $this->Student->find('all', array('conditions' => array('Student.studentgroup_id' => $selectedgroup['Studentgroup']['id'],
																												'Student.studentstate_id' => 1), 'recursive' => 0, 'order' => 'Student.id ASC'));
						foreach ($students as $student):
							$student['facultet'] = $facultet['Facultet']['name'];
							$student['profession'] =  $selectedprofession['Profession']['name'];
							$student['course'] = $selectedcourse['Course']['name'];
							$student['group'] = $selectedgroup['Studentgroup']['name'];
							array_push($preparedstudents, $student);
						endforeach;
					endforeach;
				endforeach;
			endforeach;
		endforeach;
		$this->set('students', $preparedstudents);		
	}
	
	/*
	 * генерирует файл word с бланками экзаменационных листов отмеченных сдудентов 
	 */
	function examlist() {
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		$this->loadModel('Student');
		
		$tmpstudents_ids = $this->data['students'];
		$students_ids = array();
		foreach($tmpstudents_ids as $key => $value):
		if ($value){
			array_push($students_ids, $value);
		}
		endforeach;
		
		$students = $this->Student->find('all', array('conditions' => array('Student.id' => $students_ids)));
		
		$allstudents = array();
		
		foreach ($students as $student):
		$group = $this->Studentgroup->find('first', array('conditions' => array('Studentgroup.id' => $student['Student']['studentgroup_id']), 'recursive' => 0));
		//debug($group);
		$course = $this->Course->find('first', array('conditions' => array('Course.id' => $group['Studentgroup']['course_id']), 'recursive' => 0));
		//debug($course);
		$profession = $this->Profession->find('first', array('conditions' => array('Profession.id' => $course['Course']['profession_id']), 'recursive' => 0));
		//debug($profession);
		$student['facultet'] = $profession['Facultet']['name'];
		$student['profession'] = $profession['Profession']['name'];
		$student['course'] = $course['Course']['name'];
		$student['group'] = $group['Studentgroup']['name'];
			
		array_push($allstudents, $student);
		endforeach;
		
		$this->set('students', $allstudents);
		$this->render('/', 'blank', 'examlist');		
	}
	
	/********************************************
	 * формироавние приказов стартовая страница
	 ********************************************/
	function ordersindex(){
		
	}
	
	/*
	 * выгружает файл приказа по форме
	 */
	function orderblank() {
/* 			$orderform = $this->data['Document']['orderform'];
			$this->set('i', $orderform);
			$this->render('/', 'blank', 'orderblankfromfile');	 */

		$this->autoRender = false;
		$this->view = 'Media';
		$params = array(
		      	'id' => '111.txt',
		      	'name' => 'asdasd',
		  		'download' => true,
		      	'extension' => 'txt',
				'mimeType' => array('doc' => 'application/vnd.ms-word'),
		      	'path' => APP . 'files' . DS );
		$this->set($params);
		
	} 
	
	/*
	 * выводит список всех актулаьных студентов
	 */
	function allstudents() {
		$this->loadModel('Facultet');
		$this->loadModel('Profession');
		$this->loadModel('Course');
		$this->loadModel('Studentgroup');
		
		$facultets = $this->Facultet->find('all', array('recursive' => -1));
		
		$students = array();
		foreach ($facultets as $facultet):
			$professions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet['Facultet']['id']),'recursive' => -1));
			foreach ($professions as $profession):
				$courses = $this->Course->find('all', array('conditions' => array('Course.profession_id' => $profession['Profession']['id']), 'recursive' => -1));
				foreach ($courses as $course):
					$studentgroups = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.course_id' => $course['Course']['id'])));
					//debug($studentgroups);
					//die('zzz');
					foreach ($studentgroups as $studentgroup):
						$group_students = $studentgroup['Student'];
						foreach ($group_students as $group_student):
							$student = array ('id' => $group_student['id'],
											'lastname' => $group_student['lastname'],
											'firstname' => $group_student['firstname'],
											'surname' => $group_student['surname'],
											'facultet' => $facultet['Facultet']['name'],
											'profession' => $profession['Profession']['name'],
											'course' => $course['Course']['name'],
											'group' => $studentgroup['Studentgroup']['name']
									);
							array_push($students, $student);
						endforeach;
					endforeach;
				endforeach;
			endforeach;
		endforeach;
		
		$this->set('students', $students);
		
		$this->render('/', 'blank', 'allstudents');
	}
}
	
?>	